package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.SceneType;
import com.ygqh.baby.mapper.YgCouponMemberMapper;
import com.ygqh.baby.po.YgCouponMember;
import com.ygqh.baby.po.YgCouponMemberExample;
import com.ygqh.baby.service.YgCouponMemberService;
import com.ygqh.baby.utils.ArraysUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class YgCouponMemberServiceImpl implements YgCouponMemberService {
    @Autowired
    private YgCouponMemberMapper ygCouponMemberMapper;

    @Override
    public int editCouponMember(Long couponBatchId, SceneType sceneType, List<Long> relateIds, String handler) {

        List<YgCouponMember> oldMemberList = this.findMemberList(couponBatchId, sceneType);
        List<Long> oldRelateIdList = oldMemberList.stream().map(YgCouponMember::getRelateId).collect(Collectors.toList());
        ArraysUtil.removeRepet(relateIds, oldRelateIdList);

        if (CollectionUtils.isNotEmpty(oldRelateIdList)) {
            this.deleteCouponMember(couponBatchId, sceneType, oldRelateIdList, handler);
        }
        if (CollectionUtils.isNotEmpty(relateIds)) {
            List<YgCouponMember> addList = new ArrayList<>();
            for (Long relateId : relateIds) {
                YgCouponMember member = new YgCouponMember();
                member.setCouponBatchId(couponBatchId);
                member.setSceneType(sceneType.name());
                member.setRelateId(relateId);
                member.setCreateBy(handler);
                member.setCreateTime(new Date());
                member.setUpdateTime(member.getCreateTime());
                member.setUpdateBy(handler);
                member.setStatus(DataStatus.Valid.name());
                addList.add(member);
            }
            ygCouponMemberMapper.addBatch(addList);
        }
        return 1;
    }

    private void deleteCouponMember(Long couponBatchId, SceneType sceneType, List<Long> oldRelateIdList, String handler) {
        YgCouponMemberExample example = new YgCouponMemberExample();
        example.createCriteria().andCouponBatchIdEqualTo(couponBatchId).andSceneTypeEqualTo(sceneType.toString())
                .andRelateIdIn(oldRelateIdList).andStatusEqualTo(DataStatus.Valid.name());

        YgCouponMember member = new YgCouponMember();
        member.setStatus(DataStatus.Delete.name());
        member.setUpdateBy(handler);
        member.setUpdateTime(new Date());
        ygCouponMemberMapper.updateByExampleSelective(member, example);
    }

    @Override
    public List<YgCouponMember> findMemberList(Long couponBatchId, SceneType sceneType) {
        Assert.notNull(couponBatchId, "couponBatchId can't be null");
        YgCouponMemberExample example = new YgCouponMemberExample();
        YgCouponMemberExample.Criteria criteria = example.createCriteria().andCouponBatchIdEqualTo(couponBatchId).andStatusEqualTo(DataStatus.Valid.name());
        if (sceneType != null) {
            criteria.andSceneTypeEqualTo(sceneType.name());
        }
        return ygCouponMemberMapper.selectByExample(example);
    }
}
